﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;

namespace Support
{
    public class CatchError
    {
        public static void SetErrorLog()
        {
            System.Web.HttpServerUtility Server = HttpContext.Current.Server;
            string strError = "";
            //try
            //{
                Exception ex = Server.GetLastError().InnerException;
                strError = "发生时间：" + DateTime.Now.ToString()
                    + "{$br$}导致错误对象[程序集名称]：" + ex.Source
                    + "{$br$}错误信息：" + ex.Message + "{$br$}";
                strError += "堆栈信息：{$br$}" + ex.StackTrace + "{$br$}";
                strError += "发生异常的方法：" + ex.TargetSite + "{$br$}";

                strError = HttpContext.Current.Server.UrlEncode(strError);

                HttpContext.Current.Response.Redirect(string.Format("/CatchError/Index?errmsg={0}", strError));
            //}
            //catch(Exception ex)
            //{
            //    strError = HttpContext.Current.Server.UrlEncode(ex.Message);
            //    HttpContext.Current.Response.Redirect(string.Format("/CatchError/Index?errmsg={0}", strError));
            //}
        }

        public static void SetErrorLog(Exception exception)
        {
            System.Web.HttpServerUtility Server = HttpContext.Current.Server;
            try
            {
                string strFileName = DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                string strPath = Server.MapPath("/Log");
                if (!System.IO.Directory.Exists(strPath))
                {
                    System.IO.Directory.CreateDirectory(strPath);
                }
                strPath = strPath + @"\" + strFileName;
                Exception ex = exception.InnerException;//Server.GetLastError().InnerException;
                if (ex == null)
                    ex = exception;
                string strError = "发生时间:" + DateTime.Now.ToString()
                    + "\r\n导致错误对象[程序集名称]：" + ex.Source
                    + "\r\n错误信息：" + ex.Message + "\r\n";
                strError += "堆栈信息：\r\n" + ex.StackTrace + "\r\n";
                strError += "发生异常的方法：" + ex.TargetSite + "\r\n";

                if (File.Exists(strPath))
                {
                    using (StreamWriter sw = File.AppendText(strPath))
                    {
                        sw.WriteLine(strError);
                    }
                }
                else
                {
                    FileStream fs = File.Create(strPath);
                    fs.Close();
                    using (StreamWriter sw = File.CreateText(strPath))
                    {
                        sw.WriteLine(strError);
                    }
                }

            }
            catch
            {

            }
        }

        public static void SetLog(string msg)
        {
            System.Web.HttpServerUtility Server = HttpContext.Current.Server;
            try
            {
                string strFileName = DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                string strPath = Server.MapPath("/Log");
                if (!System.IO.Directory.Exists(strPath))
                {
                    System.IO.Directory.CreateDirectory(strPath);
                }
                strPath = strPath + @"\" + strFileName;

                if (File.Exists(strPath))
                {
                    using (StreamWriter sw = File.AppendText(strPath))
                    {
                        sw.WriteLine(msg);
                    }
                }
                else
                {
                    FileStream fs = File.Create(strPath);
                    fs.Close();
                    using (StreamWriter sw = File.CreateText(strPath))
                    {
                        sw.WriteLine(msg);
                    }
                }

            }
            catch
            {

            }
        }

        public static void SetShopIDLog(string msg)
        {
            System.Web.HttpServerUtility Server = HttpContext.Current.Server;
            try
            {
                string strFileName = DateTime.Now.ToString("yyyy-MM-dd") + "-shopid.txt";
                string strPath = Server.MapPath("/Log");
                if (!System.IO.Directory.Exists(strPath))
                {
                    System.IO.Directory.CreateDirectory(strPath);
                }
                strPath = strPath + @"\" + strFileName;

                if (File.Exists(strPath))
                {
                    using (StreamWriter sw = File.AppendText(strPath))
                    {
                        sw.WriteLine(msg);
                    }
                }
                else
                {
                    FileStream fs = File.Create(strPath);
                    fs.Close();
                    using (StreamWriter sw = File.CreateText(strPath))
                    {
                        sw.WriteLine(msg);
                    }
                }

            }
            catch
            {

            }
        }
    }
}
